package cn.e3mall.controller;

import java.util.HashMap;
import java.util.Map;

import javax.print.attribute.standard.Media;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.e3mall.common.utils.FastDFSClient;
import cn.e3mall.common.utils.JsonUtils;
@Controller
public class PictureController {
	
	//获取图片上传服务器的路径
	@Value("${IMAGE_SERVER_URL}")
	private String IMAGE_SERVER_URL;
	
	@RequestMapping(value="/pic/upload",produces=MediaType.TEXT_PLAIN_VALUE+"charset=UTF-8")
	@ResponseBody
	public Map fileupload(MultipartFile uploadFile) {
		try {
		//获取文件扩展名
			//获取原始的文件名
		String originalFilename = uploadFile.getOriginalFilename();
			//取扩展名
		String substring = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
		
		//创建fastFDS客户端
			FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/client.conf");
			
		//上传文件
			String path = fastDFSClient.uploadFile(uploadFile.getBytes(), substring);
		//构建返回路径
				String url = IMAGE_SERVER_URL+path;
		//创建一个MAP返回
			Map map = new HashMap<>();
			map.put("error", 0);
			map.put("url", url);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Map  map = new HashMap<>();
			map.put("error", 1);
			map.put("message", "图片上传失败");
			return map;
		}
		
	}
}
